<?php

class SettingController extends GxController {


	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Setting'),
		));
	}

	public function actionCreate() {
		$model = new Setting;


		if (isset($_POST['Setting'])) {
			$model->setAttributes($_POST['Setting']);
			$model->updated_at = new CDbExpression('NOW()');
			if ($model->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('settings'));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'Setting');


		if (isset($_POST['Setting'])) {
			$model->setAttributes($_POST['Setting']);
			$model->updated_at = new CDbExpression('NOW()');

			if ($model->save()) {
				$this->redirect(array('settings'));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}
	
	public function actionSettings() {
		$models = Setting::model()->findAll();
		
		if(isset($_POST['Setting'])) {
			$post = $_POST['Setting'];
			$data = array();
			$hasErrors = false; 
				
			for($i = 0; $i < count($models); $i++) {
				$models[$i]->setAttributes($post[$models[$i]->name]);
				$models[$i]->setScenario('settings');
				
				$models[$i]->save();
			}			
		}
		
		$this->render('settings', array(
			'settings' => $models,
		));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'Setting')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Setting', array(
			'pagination'=>array('pageSize'=>20,),
			'sort'=>array('defaultOrder' => 'id'),
			));
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new Setting('search');
		$model->unsetAttributes();

		if (isset($_GET['Setting']))
			$model->setAttributes($_GET['Setting']);

		$this->render('admin', array(
			'model' => $model,
		));
	}

}
